﻿Imports Ocean.OceanFramework.WPF
Partial Public Class EditContactsAjaxComboBoxSelectionView

    Private WithEvents _objEditContactsAjaxComboBoxSelectionViewModel As EditContactsAjaxComboBoxSelectionViewModel

    Private Sub _objEditContactsAjaxComboBoxSelectionViewModel_ClearAjaxComboBox(ByVal sender As Object, ByVal e As System.EventArgs) Handles _objEditContactsAjaxComboBoxSelectionViewModel.ClearAjaxComboBox

        If Me.cboLastNameSearch IsNot Nothing Then
            Me.cboLastNameSearch.ClearAll()
        End If

    End Sub

    ''' <summary>
    ''' this is a custom event fired by our AjaxStyleComboBox
    ''' it's requesting new data so we provide it
    ''' </summary>
    Private Sub cboLastNameSearch_LoadItemsSource(ByVal sender As Object, ByVal e As LoadItemsSourceRoutedEventArgs) Handles cboLastNameSearch.LoadItemsSource

        If _objEditContactsAjaxComboBoxSelectionViewModel IsNot Nothing Then
            Me.cboLastNameSearch.ItemsSource = _objEditContactsAjaxComboBoxSelectionViewModel.Search(e.SearchString)
        End If

    End Sub

    ''' <summary>
    ''' this is a custom event fired by our AjaxStyleComboBox
    ''' a record has been selected, now it's up to the local
    ''' code to act upon a record selection
    ''' </summary>
    Private Sub cboLastNameSearch_RecordSelected(ByVal sender As Object, ByVal e As RecordSelectedRoutedEventArgs) Handles cboLastNameSearch.RecordSelected

        If _objEditContactsAjaxComboBoxSelectionViewModel IsNot Nothing Then

            If _objEditContactsAjaxComboBoxSelectionViewModel.VerifyDataBeforeClosing Then
                _objEditContactsAjaxComboBoxSelectionViewModel.Select(CType(e.Value, Integer))
                Keyboard.Focus(Me.objContactView.txtFirstName)

            Else
                Me.cboLastNameSearch.ClearAll()
            End If

        End If

    End Sub

    Private Sub EditContactsAjaxComboBoxSelectionView_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        _objEditContactsAjaxComboBoxSelectionViewModel = TryCast(Me.DataContext, EditContactsAjaxComboBoxSelectionViewModel)
    End Sub

    Private Sub EditContactsAjaxComboBoxSelectionView_Unloaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Unloaded
        _objEditContactsAjaxComboBoxSelectionViewModel = Nothing
    End Sub

End Class
